import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test08 {
    public static void main(String[] args) {
        int[] arr = new int[]{1,0,2,3,0,4,5,0};
        duplicateZeros(arr);
        System.out.println("111");
    }

    public static void duplicateZeros(int[] arr) {
        int dev = 0;
        int dest = -1;
        Map<Integer,Integer> map = new HashMap<>();
        List<Integer> list = new ArrayList<>();
        while(dev<arr.length){
            if(arr[dev]==0){
                dest+=2;
            }
            else{
                dest+=1;
            }
            if(dest>=arr.length-1){
                break;
            }
            dev++;
        }
        if(dest==arr.length){
            arr[dest-1] = 0;
            dev--;
            dest-=2;
        }

        int newLast = dev;    //这个用来表示复写0之后最后一个元素在原数组的下标

        while(newLast>=0){
            if(arr[newLast]==0){
                arr[dest] = 0;
                arr[dest-1] = 0;
                dest-=2;
            }else{
                arr[dest] = arr[newLast];
                dest--;
            }
            newLast--;
        }
    }

}
